package zdream.control.foe.mm;

import zdream.control.world.buff.AttackModule;
import zdream.control.world.MFoe;
import zdream.control.world.buff.HealthModule;
import zdream.control.world.MHookType;
import zdream.control.world.MHooks;
import zdream.control.world.TerrainHandler;
import zdream.control.world.Ticker;

import static zdream.control.world.MFoeConstants.*;

/**
 * <p>洛克人的子弹
 * <p>
 * 默认伤害为 1, 生效一次.
 * 水平移动速度为 24 块/秒.
 * <li>最长寿命为 1 秒, 如果在房间外超过 0.05 秒也会被回收.
 * <li>屏幕上最多 3 个子弹. 如果子弹出了屏幕但是仍然在房间内,
 * 不会回收, 且武器加一个空槽. (可以让你再发一颗子弹)
 * </li>
 * </p>
 *
 * @author Zdream
 * @since 0.0.1
 * @date 2020-06-10 (created)
 * @date 2022-05-31 (last modified)
 */
public class MegaManBuster extends MFoe {
	public final MegaMan parent;

	public MegaManBuster(MegaMan parent) {
		super(parent.world);
		this.parent = parent;
		type = TYPE_BULLET;
		side = parent.side;
	}

	@Override
	public String foeName() {
		return "megaman_buster";
	}

	AttackModule attackModule;
	HealthModule healthModule;

	@Override
	public void init() {
		createNormalPattern()
				.withRectangle(-1/6f, -0.125f, 1/3f, 0.25f)
				.apply();

		attackModule = buildAttackModule()
				.damage(1)
				.damageLevel(DAMAGE_LEVEL_BULLET)
				.ignoreSide(side)
				.targetTypesIsEntity()
				.stopAfterAttackOnce()
				.destroyAfterAttackReceived()
				.create();
		healthModule = buildHealthModule()
				.maxAgeInMS(1000)
				.outsideDestroyThreshold(50)
				.create();

//		sPainter = createPainter()
//				.setTextureFile("res/foes/megaman/sprites/mm7buster-sheet.json")
//				.setZIndex(657)
//				.create();
//		putPainter(sPainter);

		terrainHandler = new TerrainHandler(world);
		terrainHandler.floating = true;

		onHook(MHookType.TICK)
				.handle(this::handleMotion)
				.buildAndSave();
	}

	/* **********
	 *   盒子   *
	 ********** */

	int vx = 24, vy;

	private void handleMotion(MHooks.MHookContext rawContext) {
		Ticker ticker = world.ticker;
		if (ticker.foePaused) {
			return;
		}

		float vx = (orientation) ? this.vx : -this.vx;
		float vy = this.vy;

		velocityX = vx * ticker.duration;
		velocityY = vy * ticker.duration;

		terrainHandler.submitFloatBoxMotion(this);
	}
}
